00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef BUSVECTORMAP_HPP_
00018 #define BUSVECTORMAP_HPP_
00019
00020 #include <boost/smart_ptr/shared_ptr.hpp>
00021 #include <ga.h>
00022 #include "gridpack/parallel/parallel.hpp"
00023 #include <gridpack/parallel/distributed.hpp>
00024 #include <gridpack/component/base_component.hpp>
00025 #include <gridpack/network/base_network.hpp>
00026 #include <gridpack/math/vector.hpp>
00027
00028
00029
00030 namespace gridpack {
00031 namespace mapper {
00032
00033 template <class _network>
00034 class BusVectorMap {
00035 public:
00036
00037
00038
00039
00040
00041
00042 BusVectorMap(boost::shared_ptr<_network> network)
00043 : p_network(network)
00044 {
00045 p_Offsets = NULL;
00046 p_ISize = NULL;
00047 int iSize = 0;
00048 p_contributingBuses = NULL;
00049 p_Indices = NULL;
00050
00051 p_timer = NULL;
00052 p_timer = gridpack::utility::CoarseTimer::instance();
00053
00054 p_GAgrp = network->communicator().getGroup();
00055 p_me = GA_Pgroup_nodeid(p_GAgrp);
00056 p_nNodes = GA_Pgroup_nnodes(p_GAgrp);
00057
00058
00059 p_nBuses = p_network->numBuses();
00060
00061 contributions();
00062
00063 setBusIndexArrays();
00064 }
00065
00066 ~BusVectorMap()
00067 {
00068 if (p_Offsets != NULL) delete [] p_Offsets;
00069 if (p_ISize != NULL) delete [] p_ISize;
00070 if (p_contributingBuses != NULL) delete [] p_contributingBuses;
00071 if (p_Indices != NULL) delete [] p_Indices;
00072 }
00073
00074
00075
00076
00077
00078 boost::shared_ptr<gridpack::math::Vector> mapToVector(void)
00079 {
00080 gridpack::parallel::Communicator comm = p_network->communicator();
00081 int t_new(0), t_bus(0), t_set(0);
00082 if (p_timer) t_new = p_timer->createCategory("Vector Map: New Vector");
00083 if (p_timer) p_timer->start(t_new);
00084 boost::shared_ptr<gridpack::math::Vector>
00085 Ret(new gridpack::math::Vector(comm, p_numValues));
00086 if (p_timer) p_timer->stop(t_new);
00087 if (p_timer) t_bus = p_timer->createCategory("Vector Map: Load Bus Data");
00088 if (p_timer) p_timer->start(t_bus);
00089 loadBusData(*Ret,true);
00090 if (p_timer) p_timer->stop(t_bus);
00091 if (p_timer) t_set = p_timer->createCategory("Vector Map: Set Vector");
00092 if (p_timer) p_timer->start(t_set);
00093 Ret->ready();
00094 if (p_timer) p_timer->stop(t_set);
00095 return Ret;
00096 }
00097
00098
00099
00100
00101
00102 boost::shared_ptr<gridpack::math::RealVector> mapToRealVector(void)
00103 {
00104 gridpack::parallel::Communicator comm = p_network->communicator();
00105 int t_new(0), t_bus(0), t_set(0);
00106 if (p_timer) t_new = p_timer->createCategory("Vector Map: New Vector");
00107 if (p_timer) p_timer->start(t_new);
00108 boost::shared_ptr<gridpack::math::RealVector>
00109 Ret(new gridpack::math::RealVector(comm, p_numValues));
00110 if (p_timer) p_timer->stop(t_new);
00111 if (p_timer) t_bus = p_timer->createCategory("Vector Map: Load Bus Data");
00112 if (p_timer) p_timer->start(t_bus);
00113 loadRealBusData(*Ret,true);
00114 if (p_timer) p_timer->stop(t_bus);
00115 if (p_timer) t_set = p_timer->createCategory("Vector Map: Set Vector");
00116 if (p_timer) p_timer->start(t_set);
00117 Ret->ready();
00118 if (p_timer) p_timer->stop(t_set);
00119 return Ret;
00120 }
00121
00122
00123
00124
00125
00126
00127 gridpack::math::Vector* intMapToVector(void)
00128 {
00129 gridpack::parallel::Communicator comm = p_network->communicator();
00130 int t_new, t_bus, t_set;
00131 if (p_timer) t_new = p_timer->createCategory("Vector Map: New Vector");
00132 if (p_timer) p_timer->start(t_new);
00133 gridpack::math::Vector*
00134 Ret(new gridpack::math::Vector(comm, p_numValues));
00135 if (p_timer) p_timer->stop(t_new);
00136 if (p_timer) t_bus = p_timer->createCategory("Vector Map: Load Bus Data");
00137 if (p_timer) p_timer->start(t_bus);
00138 loadBusData(*Ret,true);
00139 if (p_timer) p_timer->stop(t_bus);
00140 if (p_timer) t_set = p_timer->createCategory("Vector Map: Set Vector");
00141 if (p_timer) p_timer->start(t_set);
00142 Ret->ready();
00143 if (p_timer) p_timer->stop(t_set);
00144 return Ret;
00145 }
00146
00147
00148
00149
00150
00151
00152 void mapToVector(gridpack::math::Vector &vector)
00153 {
00154 int t_bus(0), t_set(0);
00155 if (p_timer) t_set = p_timer->createCategory("Vector Map: Set Vector");
00156 if (p_timer) p_timer->start(t_set);
00157 vector.zero();
00158 if (p_timer) p_timer->stop(t_set);
00159 if (p_timer) t_bus = p_timer->createCategory("Vector Map: Load Bus Data");
00160 if (p_timer) p_timer->start(t_bus);
00161 loadBusData(vector,false);
00162 if (p_timer) p_timer->stop(t_bus);
00163 if (p_timer) p_timer->start(t_set);
00164 vector.ready();
00165 if (p_timer) p_timer->stop(t_set);
00166 }
00167
00168
00169
00170
00171
00172
00173 void mapToRealVector(gridpack::math::RealVector &vector)
00174 {
00175 int t_bus(0), t_set(0);
00176 if (p_timer) t_set = p_timer->createCategory("Vector Map: Set Vector");
00177 if (p_timer) p_timer->start(t_set);
00178 vector.zero();
00179 if (p_timer) p_timer->stop(t_set);
00180 if (p_timer) t_bus = p_timer->createCategory("Vector Map: Load Bus Data");
00181 if (p_timer) p_timer->start(t_bus);
00182 loadRealBusData(vector,false);
00183 if (p_timer) p_timer->stop(t_bus);
00184 if (p_timer) p_timer->start(t_set);
00185 vector.ready();
00186 if (p_timer) p_timer->stop(t_set);
00187 }
00188
00189
00190
00191
00192
00193
00194 void mapToVector(boost::shared_ptr<gridpack::math::Vector> &vector)
00195 {
00196 mapToVector(*vector);
00197 }
00198
00199
00200
00201
00202
00203
00204 void mapToRealVector(boost::shared_ptr<gridpack::math::RealVector> &vector)
00205 {
00206 mapToRealVector(*vector);
00207 }
00208
00209
00210
00211
00212
00213
00214 void mapToBus(const gridpack::math::Vector &vector)
00215 {
00216 ComplexType *values = new ComplexType[p_numValues];
00217 int t_get, t_unpack;
00218 int i, j, isize, offset;
00219 int one = 1;
00220 if (p_timer) t_get = p_timer->createCategory("mapToBus: get Data");
00221 if (p_timer) p_timer->start(t_get);
00222 vector.getElements(p_numValues, p_Indices, values);
00223 if (p_timer) p_timer->stop(t_get);
00224 ComplexType *vptr = values;
00225 if (p_timer) t_unpack = p_timer->createCategory("mapToBus: set Data");
00226 if (p_timer) p_timer->start(t_unpack);
00227 for (i=0; i<p_busContribution; i++) {
00228 p_contributingBuses[i]->setValues(vptr);
00229 isize = p_ISize[i];
00230 vptr += isize;
00231 }
00232 if (p_timer) p_timer->stop(t_unpack);
00233 delete [] values;
00234 }
00235
00236
00237
00238
00239
00240
00241 void mapToBus(const gridpack::math::RealVector &vector)
00242 {
00243 RealType *values = new RealType[p_numValues];
00244 int t_get, t_unpack;
00245 int i, j, isize, offset;
00246 int one = 1;
00247 if (p_timer) t_get = p_timer->createCategory("mapToBus: get Data");
00248 if (p_timer) p_timer->start(t_get);
00249 vector.getElements(p_numValues, p_Indices, values);
00250 if (p_timer) p_timer->stop(t_get);
00251 RealType *vptr = values;
00252 if (p_timer) t_unpack = p_timer->createCategory("mapToBus: set Data");
00253 if (p_timer) p_timer->start(t_unpack);
00254 for (i=0; i<p_busContribution; i++) {
00255 p_contributingBuses[i]->setValues(vptr);
00256 isize = p_ISize[i];
00257 vptr += isize;
00258 }
00259 if (p_timer) p_timer->stop(t_unpack);
00260 delete [] values;
00261 }
00262
00263
00264
00265
00266
00267
00268 void mapToBus(boost::shared_ptr<gridpack::math::Vector> &vector)
00269 {
00270 mapToBus(*vector);
00271 }
00272
00273 void mapToBus(boost::shared_ptr<gridpack::math::RealVector> &vector)
00274 {
00275 mapToBus(*vector);
00276 }
00277
00278 private:
00279
00280
00281
00282
00283
00284 void loadBusData(gridpack::math::Vector &vector, bool flag)
00285 {
00286 int i,idx,isize,icnt;
00287
00288 boost::shared_ptr<gridpack::component::BaseBusComponent> bus;
00289 int t_bus(0);
00290 if (p_timer) t_bus = p_timer->createCategory("loadBusData: Add Vector Elements");
00291 if (p_timer) p_timer->start(t_bus);
00292 int j;
00293 int t_pack, t_add;
00294 if (p_timer) t_pack = p_timer->createCategory("loadBusData: Fill Buffer");
00295 if (p_timer) p_timer->start(t_pack);
00296 ComplexType *vbuf = new ComplexType[p_numValues];
00297 ComplexType *vptr = vbuf;
00298 int *ibuf = new int[p_numValues];
00299 icnt = 0;
00300 for (i=0; i<p_busContribution; i++) {
00301 p_contributingBuses[i]->vectorValues(vptr);
00302 isize = p_ISize[i];
00303 idx = p_Offsets[i];
00304 for (j=0; j<isize; j++) {
00305 ibuf[icnt] = idx;
00306 idx++;
00307 icnt++;
00308 }
00309 vptr += isize;
00310 }
00311 if (p_timer) p_timer->stop(t_pack);
00312 if (p_timer) t_add = p_timer->createCategory("loadBusData: Add Elements");
00313 if (p_timer) p_timer->start(t_add);
00314 vector.addElements(p_numValues,ibuf,vbuf);
00315 if (p_timer) p_timer->stop(t_add);
00316 delete [] vbuf;
00317 delete [] ibuf;
00318 if (p_timer) p_timer->stop(t_bus);
00319 }
00320
00321
00322
00323
00324
00325
00326 void loadRealBusData(gridpack::math::RealVector &vector, bool flag)
00327 {
00328 int i,idx,isize,icnt;
00329
00330 boost::shared_ptr<gridpack::component::BaseBusComponent> bus;
00331 int t_bus(0);
00332 if (p_timer) t_bus = p_timer->createCategory("loadBusData: Add Vector Elements");
00333 if (p_timer) p_timer->start(t_bus);
00334 int j;
00335 int t_pack, t_add;
00336 if (p_timer) t_pack = p_timer->createCategory("loadBusData: Fill Buffer");
00337 if (p_timer) p_timer->start(t_pack);
00338 RealType *vbuf = new RealType[p_numValues];
00339 RealType *vptr = vbuf;
00340 int *ibuf = new int[p_numValues];
00341 icnt = 0;
00342 for (i=0; i<p_busContribution; i++) {
00343 p_contributingBuses[i]->vectorValues(vptr);
00344 isize = p_ISize[i];
00345 idx = p_Offsets[i];
00346 for (j=0; j<isize; j++) {
00347 ibuf[icnt] = idx;
00348 idx++;
00349 icnt++;
00350 }
00351 vptr += isize;
00352 }
00353 if (p_timer) p_timer->stop(t_pack);
00354 if (p_timer) t_add = p_timer->createCategory("loadBusData: Add Elements");
00355 if (p_timer) p_timer->start(t_add);
00356 vector.addElements(p_numValues,ibuf,vbuf);
00357 if (p_timer) p_timer->stop(t_add);
00358 delete [] vbuf;
00359 delete [] ibuf;
00360 if (p_timer) p_timer->stop(t_bus);
00361 }
00362
00363
00364
00365
00366
00367
00368 void loadBusData(boost::shared_ptr<gridpack::math::Vector> &vector, bool flag)
00369 {
00370 loadBusData(*vector, flag);
00371 }
00372
00373
00374
00375
00376
00377
00378 void loadRealBusData(boost::shared_ptr<gridpack::math::RealVector> &vector, bool flag)
00379 {
00380 loadRealBusData(*vector, flag);
00381 }
00382
00383
00384
00385
00386 void contributions(void)
00387 {
00388 int i;
00389
00390 int isize;
00391 p_busContribution = 0;
00392 p_numValues = 0;
00393 for (i=0; i<p_nBuses; i++) {
00394 if (p_network->getActiveBus(i)) {
00395 if (p_network->getBus(i)->vectorSize(&isize)) {
00396 p_busContribution++;
00397 p_numValues += isize;
00398 }
00399 }
00400 }
00401 p_contributingBuses
00402 = new gridpack::component::BaseBusComponent*[p_busContribution];
00403 p_ISize = new int[p_busContribution];
00404 int icnt = 0;
00405 for (i=0; i<p_nBuses; i++) {
00406 if (p_network->getActiveBus(i)) {
00407 if (p_network->getBus(i)->vectorSize(&isize)) {
00408 p_contributingBuses[icnt] = p_network->getBus(i).get();
00409 p_ISize[icnt] = isize;
00410 icnt++;
00411 }
00412 }
00413 }
00414 }
00415
00416
00417
00418
00419 void setBusIndexArrays(void)
00420 {
00421
00422 std::vector<int> nVals(p_nNodes);
00423 int i, j, nsize;
00424 for (i=0; i<p_nNodes; i++) {
00425 nVals[i] = 0;
00426 }
00427 nVals[p_me] = p_numValues;
00428
00429 char cplus[2];
00430 strcpy(cplus,"+");
00431 GA_Pgroup_igop(p_GAgrp,&nVals[0],p_nNodes,cplus);
00432
00433
00434 int offset = 0;
00435 for (i=0; i<p_me; i++) {
00436 offset += nVals[i];
00437 }
00438
00439
00440 p_Offsets = new int[p_busContribution];
00441 p_Indices = new int[p_numValues];
00442 int icnt = 0;
00443 int jcnt = offset;
00444 for (i=0; i<p_busContribution; i++) {
00445 nsize = p_ISize[i];
00446 p_Offsets[i] = jcnt;
00447 jcnt += nsize;
00448 for (j=0; j<nsize; j++) {
00449 p_Indices[icnt] = offset+icnt;
00450 icnt++;
00451 }
00452 }
00453 }
00454
00455
00456 int p_me;
00457 int p_nNodes;
00458
00459
00460 boost::shared_ptr<_network> p_network;
00461 int p_nBuses;
00462
00463
00464 int p_busContribution;
00465 int p_numValues;
00466
00467 int* p_Offsets;
00468 int* p_ISize;
00469 int* p_Indices;
00470 gridpack::component::BaseBusComponent **p_contributingBuses;
00471
00472
00473 int p_GAgrp;
00474
00475
00476 gridpack::utility::CoarseTimer *p_timer;
00477
00478 };
00479
00480 }
00481 }
00482
00483 #endif //BUSVECTORMAP_HPP_